/*
 * SplashScreen.java
 *
 * Created on May 6, 2005, 11:16 AM
 */

package com.bff.mpd.musicjukebox.ui;

import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.ui.components.MusicJDialog;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.mpd.utils.Utils;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @author  Bill
 */
public class SplashScreen extends MusicJDialog {
    private boolean stopped  = false;
    private boolean indeterminate = false;

    public SplashScreen(Frame owner, boolean modal) {
        super(owner, modal);
        setIconImage(Utils.createImageIcon(getClass(), "/resources/mpd/bjjicontray.jpg", "bjj image").getImage());
        init();
    }

    /**
     * Not modal
     * @param owner
     */
    public SplashScreen(Frame owner) {
        this(owner, false);
    }
    
    /** Creates new form SplashScreen */
    public SplashScreen(boolean modal) {
        this(new JFrame(), modal);
    }
    
    private void init() {
        initComponents();
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                setVisible(false);
                dispose();
            }
        });
        
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle window = getBounds();
        setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
        
        final Runnable closerRunner = new Runnable() {
            public void run() {
                dispose();
            }
        };
        Runnable waitRunner = new Runnable() {
            public void run() {
                try {
                    if(!loadProgressBar.isIndeterminate()) {
                        for (int i = loadProgressBar.getMinimum();i < loadProgressBar.getMaximum();i++) {
                            loadProgressBar.setValue(i);
                            if(isStopped()){
                                break;
                            }
                            
                            try {
                                Thread.sleep(50);
                            } catch (Exception e) {}
                            
                        }
                        while(!isStopped()) {
                            
                        }
                        SwingUtilities.invokeAndWait(closerRunner);
                    }
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        };
        setVisible(true);
        Thread splashThread = new Thread(waitRunner, "SplashThread");
        splashThread.start();
    }
    
    public boolean isStopped() {
        return stopped;
    }
    
    public void setStopped(boolean stopped) {
        setVisible(false);
        this.stopped = stopped;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titlePanel = new BJJPanel();
        lblTitle = new javax.swing.JLabel();
        detailsPanel = new BJJPanel();
        bottomPanel = new BJJPanel();
        loadProgressBar = new javax.swing.JProgressBar();
        lblProgressbarLabel = new javax.swing.JLabel();
        jPanel2 = new BJJPanel();
        taCenter = new javax.swing.JTextArea();
        taCenter.setText("Bill's Jammin' Jukebox\n\nVersion " + JukeboxProperties.getInstance().getVersion() + "\n\n2010\n\n\n\nBill Findeisen");
        jPanel1 = new BJJPanel();
        jLabel2 = new javax.swing.JLabel();

        setTitle("Bill's Jammin' Jukebox");

        titlePanel.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        titlePanel.setPreferredSize(new java.awt.Dimension(350, 45));

        lblTitle.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 24));
        lblTitle.setForeground(new java.awt.Color(0, 51, 255));
        lblTitle.setText("Bill's Jammin' Jukebox");
        lblTitle.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        titlePanel.add(lblTitle);

        getContentPane().add(titlePanel, java.awt.BorderLayout.NORTH);

        detailsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Details"));
        detailsPanel.setLayout(new java.awt.BorderLayout());

        bottomPanel.setBackground(new java.awt.Color(0, 0, 0));
        bottomPanel.setForeground(new java.awt.Color(255, 255, 255));
        bottomPanel.setLayout(new java.awt.BorderLayout());

        loadProgressBar.setStringPainted(true);
        bottomPanel.add(loadProgressBar, java.awt.BorderLayout.SOUTH);

        lblProgressbarLabel.setBackground(new java.awt.Color(51, 51, 51));
        lblProgressbarLabel.setForeground(new java.awt.Color(255, 255, 255));
        lblProgressbarLabel.setText("Loading...");
        bottomPanel.add(lblProgressbarLabel, java.awt.BorderLayout.WEST);

        detailsPanel.add(bottomPanel, java.awt.BorderLayout.SOUTH);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setLayout(new java.awt.BorderLayout());

        taCenter.setEditable(false);
        taCenter.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        jPanel2.add(taCenter, java.awt.BorderLayout.CENTER);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel2.setBackground(new java.awt.Color(153, 153, 153));
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/headbang.gif"))); // NOI18N
        jPanel1.add(jLabel2, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel1, java.awt.BorderLayout.WEST);

        detailsPanel.add(jPanel2, java.awt.BorderLayout.CENTER);

        getContentPane().add(detailsPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JPanel detailsPanel;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblProgressbarLabel;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JProgressBar loadProgressBar;
    private javax.swing.JTextArea taCenter;
    private javax.swing.JPanel titlePanel;
    // End of variables declaration//GEN-END:variables
    
    public void setMainTextArea(String text) {
        taCenter.setText(text);
    }
    
    public void setTitleLabel(String text) {
        lblTitle.setText(text);
    }
    
    public void setProgressBarLabel(String text) {
        lblProgressbarLabel.setText(text);
    }
    
    public boolean isIndeterminate() {
        return indeterminate;
    }
    
    public void setIndeterminate(boolean indeterminate) {
        this.indeterminate = indeterminate;
        loadProgressBar.setIndeterminate(indeterminate);
        loadProgressBar.setStringPainted(false);
    }
    
    public void setProgressbarMax(int max) {
        loadProgressBar.setMaximum(max);
    }
    
    public void setProgressbarValue(int value) {
        loadProgressBar.setValue(value);
    }
}
